<template>
  <div>
    <el-container>
      <el-header>
        <img
          class="m_logo"
          src="https://moster0506.oss-cn-shanghai.aliyuncs.com/2020/05/15/1262504573984f37b7ea100ae3d2abc0file.png"
          alt="logo"
        />
      </el-header>
      <el-main>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 表单对象
      ruleForm: {
        username: "markerHub", // 用户名
        password: "111111", // 密码
      },
      rules: {
        username: [
          { required: true, message: "请输入用户账号", trigger: "blur" },
          {
            min: 1,
            max: 30,
            message: "长度在 1 到 30 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          {
            min: 1,
            max: 30,
            message: "长度在 1 到 30 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      // 表单校验
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 保存 全局 this
          const _this = this;
          // 发送请求到 后台
          this.$axios
            .post("/login", this.ruleForm)
            .then((res) => {
              // console.log(res);
              // 凭证 jwt
              const jwt1 = res.headers["authorization"];
              // 用户信息
              const userInfo = res.data.data;
              // 放入 local，session中
              _this.$store.commit("SET_TOKEN", jwt1);
              _this.$store.commit("SET_USERINFO", userInfo);
              // 获取全局session的数据
              // console.log(_this.$store.getters.getUser);
              // 使用 router 跳转,列表
              _this.$router.push("/blogs");
            });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      // 重置
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style>
.el-header,
.el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  /* background-color: #E9EEF3; */
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.m_logo {
  height: 60%;
  margin-top: 10px;
}
.demo-ruleForm {
  max-width: 500px;
  margin: 0 auto;
}
</style>
